/*
    SUSAN® - Sole of Unix Save ANything

      Copyright (C) 2011-2016 Skyatlas Co. LTD

   

   
*/

/**
 * @file bareos storage daemon configuration definitions
 *
 */
#ifndef BAREOS_STORED_STORED_CONF_H_
#define BAREOS_STORED_STORED_CONF_H_ 1

#include "stored/dev.h"
#include "stored/autochanger_resource.h"
#include "stored/device_resource.h"
#include "lib/messages_resource.h"
#include "lib/tls_conf.h"

class alist;
class dlist;

namespace storagedaemon {

static const std::string default_config_filename("bareos-sd.conf");

/*
 * Resource codes -- they must be sequential for indexing
 */
enum
{
  R_DIRECTOR = 3001,
  R_NDMP,
  R_STORAGE,
  R_DEVICE,
  R_MSGS,
  R_AUTOCHANGER,
  R_JOB, /* needed for Job name conversion */
  R_FIRST = R_DIRECTOR,
  R_LAST = R_JOB,  /* keep this updated */
  R_CLIENT = 0xff  // dummy for bsock printing
};

enum
{
  R_NAME = 3020,
  R_ADDRESS,
  R_PASSWORD,
  R_TYPE,
  R_BACKUP
};

/* Definition of the contents of each Resource */
class DirectorResource
    : public BareosResource
    , public TlsResource {
 public:
  char* address = nullptr; /**< Director IP address or zero */
  bool monitor = false; /**< Have only access to status and .status functions */
  uint64_t max_bandwidth_per_job = 0; /**< Per director bandwidth limitation  */
  s_password keyencrkey;              /**< Key Encryption Key */

  DirectorResource() = default;
  virtual ~DirectorResource() = default;
};

class NdmpResource : public BareosResource {
 public:
  uint32_t AuthType = 0; /**< Authentication Type to use */
  uint32_t LogLevel = 0; /**< Log level to use for logging NDMP protocol msgs */
  char* username = nullptr; /**< NDMP username */
  s_password password;      /**< NDMP password */

  NdmpResource() = default;
  virtual ~NdmpResource() = default;
};

/* Storage daemon "global" definitions */
class StorageResource
    : public BareosResource
    , public TlsResource {
 public:
  dlist* SDaddrs = nullptr;
  dlist* SDsrc_addr = nullptr; /**< Address to source connections from */
  dlist* NDMPaddrs = nullptr;
  char* working_directory = nullptr; /**< Working directory for checkpoints */
  char* pid_directory = nullptr;
  char* subsys_directory = nullptr;
  char* plugin_directory = nullptr; /**< Plugin directory */
  alist* plugin_names = nullptr;
  char* scripts_directory = nullptr;
  std::vector<std::string> backend_directories;
  uint32_t MaxConcurrentJobs = 0;      /**< Maximum concurrent jobs to run */
  uint32_t MaxConnections = 0;         /**< Maximum connections to allow */
  uint32_t ndmploglevel = 0;           /**< Initial NDMP log level */
  uint32_t jcr_watchdog_time = 0;      /**< Absolute time after which a Job gets
                                      terminated regardless of its progress */
  uint32_t stats_collect_interval = 0; /**<  in seconds */
  MessagesResource* messages = nullptr; /**< Daemon message handler */
  utime_t SDConnectTimeout = {0};       /**< Timeout in seconds */
  utime_t FDConnectTimeout = {0};       /**< Timeout in seconds */
  utime_t heartbeat_interval = {0};     /**< Interval to send hb to FD */
  utime_t client_wait = {0};            /**< Time to wait for FD to connect */
  uint32_t max_network_buffer_size = 0; /**< Max network buf size */
  bool autoxflateonreplication =
      false;               /**< Perform autoxflation when replicating data
                            */
  bool compatible = false; /**< Write compatible format */
  bool allow_bw_bursting = false; /**< Allow bursting with bandwidth limiting */
  bool ndmp_enable = false;       /**< Enable NDMP protocol listener */
  bool ndmp_snooping = false;     /**< Enable NDMP protocol snooping */
  bool collect_dev_stats = false; /**< Collect Device Statistics */
  bool collect_job_stats = false; /**< Collect Job Statistics */
  bool device_reserve_by_mediatype = false; /**< Allow device reservation based
                                       on a matching mediatype */
  bool filedevice_concurrent_read = false;  /**< Allow filedevices to be read
                                       concurrently */
  char* verid = nullptr; /**< Custom Id to print in version command */
  char* secure_erase_cmdline = nullptr; /**< Cmdline to execute to perform
                                 secure erase of file */
  char* log_timestamp_format = nullptr; /**< Timestamp format to use in generic
                                 logging messages */
  uint64_t max_bandwidth_per_job = 0;   /**< Bandwidth limitation (global) */

  StorageResource() = default;
  virtual ~StorageResource() = default;
};

ConfigurationParser* InitSdConfig(const char* configfile, int exit_code);
bool ParseSdConfig(const char* configfile, int exit_code);
bool PrintConfigSchemaJson(PoolMem& buffer);

} /* namespace storagedaemon */
#endif /* BAREOS_STORED_STORED_CONF_H_ */
